/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon
 												
Class							:				CaseBaseEpisode
 
Function						: 				
*********************************************************************************/

package cbr.episodicCBR;

import org.jdom.Element;

import util.XMLWriter;
import base.WargusGameState;

public class CaseBaseEpisode {
	
	String m_behaviorID = null;
	WargusGameState m_gameState = null;
	float m_outcome = 0.0f;
	
	public CaseBaseEpisode(String bID,WargusGameState gameState,float outcome) {
		m_behaviorID = bID;
		m_gameState = gameState;
		m_outcome = outcome;
	}
	
	/*
	 * This method saves an episode to a file in XML format
	 */
	public void saveToXML(XMLWriter w)
	{
		w.tag("episode");
		{
			w.tag("behavior", m_behaviorID);
			m_gameState.writeXML(w);
			
			w.tag("outcome");
			{
				w.tag("player", m_gameState.getPlayer().getID());
				w.tag("success", m_outcome);
			}
			w.tag("/outcome");
		}
		w.tag("/episode");
	}
	
	public static CaseBaseEpisode loadFromXML(Element e) {
		assert e.getName().equals("episode") :
			"CaseBaseEpisode.loadFromXML: Invalid XML Element " + e.getName();
		
		String behaviorID = e.getChildText("behavior");
		WargusGameState state = WargusGameState.loadFromXML(e.getChild("description"));
		float success = Float.parseFloat(e.getChild("outcome").getChildText("success"));
		
		return new CaseBaseEpisode(behaviorID, state, success);
	}
}
